/*
编写 : Hexh
时间 : 2012-6-15 11:25:39
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsCarInventoryList
	/// </summary>
	public class FsCarInventoryListController : BaseController
	{

		private readonly IFsCarInventoryListFacade FsCarInventoryListFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public FsCarInventoryListController(IFsCarInventoryListFacade FsCarInventoryListFacade, IHospUserFacade HospUserFacade)
		{
			this.FsCarInventoryListFacade = FsCarInventoryListFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[Description("访问FsCarInventoryList管理页面")]
		[ViewPage]
		public ActionResult FsCarInventoryList()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改FsCarInventoryList信息")]
		public ActionResult SaveOrUpdate()
		{
			FsCarInventoryList entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsCarInventoryList
				{
					InveId = Request["InveId"], 
					CarId = Request["CarId"], 
					State = Request["State"]==null ? 0 : int.Parse(Request["State"]), 
					OperUserId = Request["OperUserId"], 
					OperDate = Request["OperDate"], 
				};
			}
			else
			{
				// get the entity.
				entity = FsCarInventoryListFacade.Get(Request["Id"]);
				// set new value..
				entity.InveId = Request["InveId"]; 
				entity.CarId = Request["CarId"]; 
				entity.State = Request["State"]==null ? 0 : int.Parse(Request["State"]); 
				entity.OperUserId = Request["OperUserId"]; 
				entity.OperDate = Request["OperDate"]; 
			}			
			// TODO ..设置外键相关信息..
			/*
			string[] authusers = Request["AuthorizeUsers"].Split(',');
			for (int i = 0; i < authusers.Length; i++)
			{
				entity.AuthorizeUsers.Add(HospUserFacade.Load(authusers[i]));
			}
			*/
			// 保存信息
			FsCarInventoryListFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除FsCarInventoryList基本资料")]
		public ActionResult Delete()
		{
			FsCarInventoryListFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除FsCarInventoryList基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsCarInventoryListFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsCarInventoryList清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsCarInventoryListFacade.GetPlist(
				Request["inventoryId"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsCarInventoryList> { total = total, data = data },
				new string[] { "OperUser", "Car" });
		}
		
		[Description("获取FsCarInventoryList信息")]
		public ActionResult GetById()
		{
			var entity = FsCarInventoryListFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsCarInventoryList> { data = new FsCarInventoryList[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断FsCarInventoryList字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsCarInventoryListFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
